package com.zsh.wanandroid.ui.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.bean.ArticleBean;
import com.zsh.wanandroid.bean.ArticleTags;

import java.util.List;

/**
 * @author zsh
 * @date 2020/3/13.
 * 功能：主页面adapter
 */

public class HomeAdapter extends BaseQuickAdapter<ArticleBean<ArticleTags>, BaseViewHolder> {

    private static long ONE_DAY = 1 * 24 * 60 * 60 * 1000; //1天

    public HomeAdapter(@LayoutRes int layoutResId, @Nullable List<ArticleBean<ArticleTags>> data) {
        super(layoutResId, data);
    }

    public HomeAdapter(@LayoutRes int layoutResId) {
        super(layoutResId);
    }

    @Override
    protected void convert(BaseViewHolder helper, ArticleBean<ArticleTags> item) {
        helper.getView(R.id.tvMark1).setVisibility(View.GONE);//是否为置顶
        helper.getView(R.id.tvMark2).setVisibility(View.GONE);//是否为新
        helper.getView(R.id.tvMark3).setVisibility(View.GONE);//是否为项目
        helper.getView(R.id.tvMark4).setVisibility(View.GONE);//是否为本站发布
        helper.getView(R.id.tvMark5).setVisibility(View.GONE);//是否为公众号
        helper.getView(R.id.ivPic).setVisibility(View.GONE);
        //标题栏，是否置顶
        if (item.getType() == 1) {
            helper.setVisible(R.id.tvMark1, true);
            helper.setText(R.id.tvMark1, "置顶");
            helper.setTextColor(R.id.tvMark1, mContext.getResources().getColor(R.color.colorRed));
            helper.setBackgroundRes(R.id.tvMark1, R.drawable.red_frame);
        }
        //标题栏，是否为新文章
        //分享时间
        long shareDate = item.getShareDate();
        //获取当前时间
        long nowMills = TimeUtils.getNowMills();
        if ((nowMills - shareDate) > ONE_DAY) {
            //分享时间大于1天
        } else {
            helper.setVisible(R.id.tvMark2, true);
            helper.setText(R.id.tvMark2, "新");
            helper.setTextColor(R.id.tvMark2, mContext.getResources().getColor(R.color.colorRed));
            helper.setBackgroundRes(R.id.tvMark2, R.drawable.red_frame);
        }
        //标题栏，是否为项目
        if (!item.getEnvelopePic().equals("")) {
            helper.setVisible(R.id.tvMark3, true);
            helper.setText(R.id.tvMark3, "项目");
            helper.setTextColor(R.id.tvMark3, mContext.getResources().getColor(R.color.colorTheme2));
            helper.setBackgroundRes(R.id.tvMark3, R.drawable.blue_frame);
        }
        //标题栏，是否为本站发布
        if (item.getEnvelopePic().equals("官方")) {
            helper.setVisible(R.id.tvMark4, true);
            helper.setText(R.id.tvMark4, "本站发布");
            helper.setTextColor(R.id.tvMark4, mContext.getResources().getColor(R.color.colorTheme2));
            helper.setBackgroundRes(R.id.tvMark4, R.drawable.blue_frame);
        }
        //标题栏，是否为公众号
        if (item.getSuperChapterName().equals("公众号")) {
            helper.setVisible(R.id.tvMark5, true);
            helper.setText(R.id.tvMark5, item.getSuperChapterName());
            helper.setTextColor(R.id.tvMark5, mContext.getResources().getColor(R.color.colorTheme2));
            helper.setBackgroundRes(R.id.tvMark5, R.drawable.blue_frame);
        }
        //图片，是否为项目
        if (!item.getEnvelopePic().equals("")) {
            helper.setVisible(R.id.ivPic, true);
            Glide.with(mContext).load(item.getEnvelopePic()).centerCrop().into((ImageView) helper.getView(R.id.ivPic));
        }
        //作者
        String author = item.getAuthor();
        String shareUser = item.getShareUser();
        helper.setText(R.id.tvShareUser, !author.equals("") ? author : shareUser);
        //时间
        helper.setText(R.id.tvTime, item.getNiceDate());
        //标题
        helper.setText(R.id.tvTitle, item.getTitle());
        //分类
        helper.setText(R.id.tvChapterName, item.getSuperChapterName() + " / " + item.getChapterName());
        //是否收藏
        if (item.isCollect()) {
            helper.setBackgroundRes(R.id.ivLike, R.drawable.ic_like);
        } else {
            helper.setBackgroundRes(R.id.ivLike, R.drawable.ic_like_not);
        }
    }
}
